//go:build oracle
// +build oracle

package sql

import _ "gitee.com/micro-plat/qtask/internal/modules/const/sql/oracle"

const SQLGetSEQ = `select seq_system_task_id.nextval from dual`

const SQLGetBatch = `select seq_system_task_id.nextval from dual`

const SQLQueryTaskForInsert = `
select t.task_id from tsk_system_task t 
where t.order_no = @order_no
and t.queue_name = @queue_name
and t.name = @name
and t.plat_name = @plat_name
&t.ext_no
and t.status in(20,30)
and rownum <= 1
`

const SQLCreateTask = `
insert into tsk_system_task
  (task_id,
   name,
   plat_name,
   next_execute_time,
   max_execute_time,
   next_interval,
   delete_interval,
   status,
   max_count,
   order_no,
   ext_no,
   queue_name,
   msg_content)
values
  (@task_id,
   @name,
   @plat_name,
   sysdate + #first_timeout / 24 / 60 / 60,
   sysdate + #max_timeout / 24 / 60 / 60,
   @next_interval,
   @delete_interval,
   20,
   @max_count,
   @order_no,
   @ext_no,
   @queue_name,
   @msg_content)
`

const SQLProcessingTask = `
update tsk_system_task t set 
t.last_update_time = sysdate,
t.next_execute_time=sysdate+t.next_interval/24/60/60,
t.status=30,
t.count=t.count + 1,
t.last_execute_time=sysdate
where  t.task_id=@task_id 
and t.status in(20,30)
`

const SQLFinishTask = `
update tsk_system_task t set 
    t.last_update_time = sysdate,
    t.next_execute_time = to_date('2099-12-31', '%yyyy-%mm-%dd'),
    t.status            = 0,
    t.delete_time       = sysdate + t.delete_interval / 24 / 60 / 60
where t.task_id = @task_id
and t.status in (20, 30)`

const SQLUpdateTask = `
update tsk_system_task t set 
t.last_update_time = sysdate,
t.batch_id=@batch_id,
t.next_execute_time= sysdate+t.next_interval/24/60/60
where  t.max_execute_time > sysdate
and t.next_execute_time <= sysdate 
and t.status in(20,30) 
and t.plat_name = @plat_name
and (t.count < t.max_count or t.max_count=0)
and rownum <= 200`

const SQLQueryWaitProcess = `
select queue_name,msg_content 
from 
tsk_system_task t 
where t.batch_id=@batch_id
and t.next_execute_time > sysdate
`

const SQLClearTask = `delete from tsk_system_task where delete_time < sysdate and status in (0, 90)`

const SQLFailedTask = `
update tsk_system_task t set 
t.last_update_time = sysdate,
t.delete_time = sysdate + decode(t.delete_interval, 0, 604800, t.delete_interval) / 24 / 60 / 60,
t.status = 90
where ((t.max_execute_time > sysdate - 7
and t.max_execute_time < sysdate) or (t.count >= t.max_count and t.max_count!=0)))
and t.status in (20, 30)
and rownum <= 5000
`

const SQLClosetask = `
update tsk_system_task t set 
    t.last_update_time = sysdate,
    t.next_execute_time = to_date('2099-12-31', '%yyyy-%mm-%dd'),
    t.status            = 91,
    t.delete_time       = sysdate + t.delete_interval / 24 / 60 / 60
where t.task_id = @task_id
AND t.status IN (20, 30)
`

// SQLUpdateTaskByTaskID 更新任务通过id
const SQLUpdateTaskByTaskID = `
UPDATE tsk_system_task t SET 
 t.last_update_time = sysdate,
 t.next_execute_time = sysdate + nvl(@first_timeout,0)/24/60/60,
 t.max_execute_time =decode(@max_timeout,'',t.max_execute_time, sysdate + nvl(@max_timeout,0)/24/60/60),
 t.next_interval = decode(@next_interval,'',t.next_interval,@next_interval),
 t.max_count = decode(@max_count,'',t.max_count,t.count + @max_count),
 t.msg_content = decode(@msg_content,'',t.msg_content,@msg_content)
WHERE t.task_id = @task_id
AND t.status IN (20, 30)
`
const SQLTaskRepeat = `
UPDATE tsk_system_task t SET 
t.last_update_time = sysdate,
t.next_execute_time = sysdate + #first_timeout / 24 / 60 / 60, 
t.msg_content=nvl(@msg_content,t.msg_content),
t.max_execute_time = sysdate + #max_timeout / 24 / 60 / 60
where t.task_id=@task_id
AND t.status IN (20, 30)
`
const SQLQueryTaskByID = `
select count(1) from tsk_system_task t where t.task_id = @task_id
`
